package proxy;

import java.lang.reflect.Method;

public class GPJavaProxy implements GPInvocationHandler{
    private Dog target;
    public Dog getInstance(Dog target){
        this.target=target;
        Class<?> clazz=target.getClass();
        return (Dog) GPProxy.newProxyInstance(new GPClassLoader(),clazz.getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result=method.invoke(this.target,args);
        after();
        return result;
    }

    private void after() {
        System.out.println("se");
    }

    private void before() {
        System.out.println("bb");
    }

    public static void main(String[] args) {
        GPJavaProxy gpJavaProxy=new GPJavaProxy();
        Dog dog=gpJavaProxy.getInstance(new GunDog());
        dog.run();
    }
}
